package jrain.fw.business.common;

import java.util.function.Consumer;

import jrain.fw.core.business.BusinessRequest;
import jrain.fw.core.business.BusinessResponse;
import jrain.fw.core.business.client.BusinessInnerClient;
import jrain.fw.core.log.Logger;
import jrain.fw.core.log.LoggerFactory;
import jrain.fw.core.service.Service;
import jrain.fw.core.service.ServiceConst;
import jrain.fw.core.utils.FwUtils;
import jrain.utils.date.DateUtils;

@Service(group = ServiceConst.S_BUSINESS_INNER_CLIENT, module = ServiceConst.M_BUSINESS)
public class BusinessInnerClientService implements BusinessInnerClient {

	private static final Logger log = LoggerFactory.getLogger("business-inner-client");

	@Override
	public BusinessResponse doSync(BusinessRequest request) {
		long start = System.currentTimeMillis();
		BusinessResponse r = FwUtils.getBusinessProvider().doSync(request);
		log.debug("business={},time={},code={},msg={}", request.getKey(), DateUtils.calculation(start), r.getCode(), r.getMsg());
		return r;
	}

	@Override
	public void doAsync(BusinessRequest request, Consumer<BusinessResponse> callbak) {
		long start = System.currentTimeMillis();
		FwUtils.getBusinessProvider().doAsync(request, new Consumer<BusinessResponse>() {
			@Override
			public void accept(BusinessResponse r) {
				log.debug("business={},time={},code={},msg={}", request.getKey(), DateUtils.calculation(start), r.getCode(), r.getMsg());
				callbak.accept(r);
			}
		});
	}

}
